package com.example.demo20;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView listView1,listView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView1=findViewById(R.id.list_text);
        listView2=findViewById(R.id.list_simple);
        final String[] lang={"c++","java","python","c"};
        ArrayAdapter adapter=new ArrayAdapter(this,R.layout.item_text_layout,lang);
        listView1.setAdapter(adapter);
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),lang[position],Toast.LENGTH_SHORT).show();
            }
        });
        final int[] images = {R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
        final String[] names={"李白","杜甫","白居易","骆宾王"};
        List list=new ArrayList();
        for (int i = 0; i < images.length; i++) {
            HashMap<String,Object> map=new HashMap<>();
            map.put("NAME",names[i]);
            map.put("IMAGE",images[i]);
            list.add(map);
        }

        SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.item_simple_layout,new String[]{"NAME","IMAGE"},new int[]{R.id.item_simple_name,R.id.item_simple_image});
        listView2.setAdapter(simpleAdapter);
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),names[position],Toast.LENGTH_SHORT).show();
            }
        });
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Intent intent=new Intent(MainActivity.this,ExMainActivity.class);
        startActivity(intent);
    }
}
